package org.PropertyManageMentSystem.Manager;

import org.PropertyManageMentSystem.Data.Comment;
import org.PropertyManageMentSystem.Data.Complaint;
import org.PropertyManageMentSystem.Data.MysqlConfig;

import java.sql.*;
import java.util.ArrayList;

public class ComplaintManager {
    private Connection conn;

    public ComplaintManager(MysqlConfig config) {
        String url = config.getUrl();
        String user = config.getUser();
        String password = config.getPassword();

        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            //创建连接
            this.conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Long addComplaint(Long taskId, String info) {
        try {
            String sql = "Insert into complaint (taskId, info) values(?, ?)";
            PreparedStatement stat = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);//SQL语句已预编译并存储在PreparedStatement对象中。

            stat.setLong(1, taskId); //即将p.getName()所获得值赋给第1个问号的地方
            stat.setString(2, info);

            //执行sql语句
            stat.executeUpdate();
            ResultSet rs = stat.getGeneratedKeys();
            Long keyid = -1l;
            if(rs.next()){
                keyid = rs.getLong(1);
            }
            return keyid;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public ArrayList<Complaint> getAllComplaint() {
        ArrayList<Complaint> complaints = new ArrayList<Complaint>();
        try {
            String sql = "select * from complaint";
            PreparedStatement stat = conn.prepareStatement(sql);

            //因为executeQuery会有ResultSet的返回值
            ResultSet res = stat.executeQuery();

            //将res中的值取出给到集合里面
            while (res.next()) {
                Long complaintId = res.getLong("complaintId");
                Long taskId = res.getLong("taskId");
                String info = res.getString("info");
                int stateNum = res.getInt("state");
                String result = res.getString("result");

                Complaint.ComplaintState state = Complaint.ComplaintState.values()[stateNum];
                Complaint comp = new Complaint(complaintId, taskId, info, state, result);
                //添加到容器
                complaints.add(comp);
            }
            return complaints;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Complaint getComplaintById(Long complaintId) {
        System.out.println(complaintId);
        try {
            String sql = "select * from complaint where complaintId=?";
            PreparedStatement stat = conn.prepareStatement(sql);
            stat.setLong(1, complaintId);

            //因为executeQuery会有ResultSet的返回值
            ResultSet res = stat.executeQuery();

            //将res中的值取出给到集合里面
            if (res.next()) {
                Long taskId = res.getLong("taskId");
                String info = res.getString("info");
                int stateNum = res.getInt("state");
                String result = res.getString("result");

                Complaint.ComplaintState state = Complaint.ComplaintState.values()[stateNum];
                return new Complaint(complaintId, taskId, info, state, result);
            } else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void updateStateById(Long complaintId, Complaint.ComplaintState state) {
        try {
            String sql = "update complaint set state=? where complaintId=?";
            PreparedStatement stat = conn.prepareStatement(sql);

            int stateNum = state.ordinal();
            stat.setInt(1, stateNum);
            stat.setLong(2, complaintId);
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void finishComplaint(Long complaintId) {
        try {
            String sql = "update complaint set state=? where complaintId=?";
            PreparedStatement stat = conn.prepareStatement(sql);

            int stateNum = Complaint.ComplaintState.closed.ordinal();
            stat.setInt(1, stateNum);
            stat.setLong(2, complaintId);
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
